package ui;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ListSelectionModel;

import java.awt.GridBagLayout;
import javax.swing.JToolBar;
import javax.swing.JList;
import java.awt.GridBagConstraints;
import javax.swing.JTextPane;
import javax.swing.JPanel;
import java.awt.Insets;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.border.LineBorder;
import java.awt.Color;


/**
 * 
 * @author kbza
 *
 *  Frame implementado con Swing.
 */
public class TVFGUI extends JFrame implements ActionListener {

	private JFrame frame = this;
	private JMenuItem mntmCargarDirectorio;
	private JMenuItem mntmCargarArchivo;
	private JList list;
	private DefaultListModel listModel;
	private File[] files;
	private JPanel botoneraPanel;

	public TVFGUI() {

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnArchivos = new JMenu("Archivos");
		menuBar.add(mnArchivos);

		mntmCargarDirectorio = new JMenuItem("Cargar Directorio...");
		mntmCargarDirectorio.addActionListener(this);
		mnArchivos.add(mntmCargarDirectorio);

		mntmCargarArchivo = new JMenuItem("Cargar Archivo...");
		mntmCargarArchivo.addActionListener(this);
		mnArchivos.add(mntmCargarArchivo);

		JMenu mnAyuda = new JMenu("Ayuda");
		menuBar.add(mnAyuda);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 185, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 16, 175, 57, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);

		list = new JList();
		listModel = new DefaultListModel();
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setModel(listModel);
		list.setBorder(new TitledBorder(null, "Archivos", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_list = new GridBagConstraints();
		gbc_list.gridheight = 2;
		gbc_list.insets = new Insets(0, 0, 5, 5);
		gbc_list.fill = GridBagConstraints.BOTH;
		gbc_list.gridx = 0;
		gbc_list.gridy = 0;
		getContentPane().add(list, gbc_list);
		
		MouseListener mouseListener = new MouseAdapter() {
		    public void mouseClicked(MouseEvent e) {
		        if (e.getClickCount() == 2) {
		            int index = list.locationToIndex(e.getPoint());
		            System.out.println("Entra a: "+index);
		            
		            JPanel treePanel = new JTreeView().getPanel(files[index].getAbsolutePath());
		            treePanel.setBorder(new TitledBorder(null, files[index].getName(), TitledBorder.CENTER, TitledBorder.ABOVE_TOP, null, null));
		            GridBagConstraints gbc_treePanel = new GridBagConstraints();
		    		gbc_treePanel.insets = new Insets(0, 0, 5, 0);
		    		gbc_treePanel.gridheight = 2;
		    		gbc_treePanel.fill = GridBagConstraints.BOTH;
		    		gbc_treePanel.gridx = 1;
		    		gbc_treePanel.gridy = 0;
		    		getContentPane().add(treePanel, gbc_treePanel);
		    		GroupLayout gl_treePanel = new GroupLayout(treePanel);
		    		gl_treePanel.setHorizontalGroup(
		    			gl_treePanel.createParallelGroup(Alignment.LEADING)
		    				.addGap(0, 292, Short.MAX_VALUE)
		    		);
		    		gl_treePanel.setVerticalGroup(
		    			gl_treePanel.createParallelGroup(Alignment.LEADING)
		    				.addGap(0, 249, Short.MAX_VALUE)
		    		);
		    		treePanel.setLayout(gl_treePanel);
		            
		            System.out.println("Sale: "+index);
		            
		            
		         }
		    }
		};
		list.addMouseListener(mouseListener);
		
		botoneraPanel = new JPanel();
		botoneraPanel.setBorder(new TitledBorder(null, "Navegaci\u00F3n", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_botoneraPanel = new GridBagConstraints();
		gbc_botoneraPanel.gridwidth = 2;
		gbc_botoneraPanel.anchor = GridBagConstraints.NORTH;
		gbc_botoneraPanel.insets = new Insets(0, 0, 0, 5);
		gbc_botoneraPanel.fill = GridBagConstraints.HORIZONTAL;
		gbc_botoneraPanel.gridx = 0;
		gbc_botoneraPanel.gridy = 2;
		getContentPane().add(botoneraPanel, gbc_botoneraPanel);
		
		JButton btnInicio = new JButton("Inicio");
		
		JButton btnAnterior = new JButton("Anterior");
		
		JButton btnSiguiente = new JButton("Siguiente");
		
		JButton btnFin = new JButton("Fin");
		btnInicio.setSize(btnSiguiente.getSize());
		btnAnterior.setSize(btnSiguiente.getSize());
		btnFin.setSize(btnSiguiente.getSize());
		botoneraPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		botoneraPanel.add(btnInicio);
		botoneraPanel.add(btnAnterior);
		botoneraPanel.add(btnSiguiente);
		botoneraPanel.add(btnFin);
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(mntmCargarArchivo)) {
			JFileChooser fc = new JFileChooser("/home/kbza/workspace/FAIAUI/searchTrees/");
			fc.setMultiSelectionEnabled(true);
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					"Archivo del Arbol", "xml");
			fc.setFileFilter(filter);
			int respuesta = fc.showOpenDialog(this);
			if (respuesta == JFileChooser.APPROVE_OPTION) {
				setScreen(fc.getSelectedFiles());
				mostrarArchivos(fc.getSelectedFiles());
			}

		}
		
		if (e.getSource().equals(mntmCargarDirectorio)) {
			JFileChooser fc = new JFileChooser("./");
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					"Archivo del Arbol", "xml");
			fc.setFileFilter(filter);
			int respuesta = fc.showOpenDialog(this);
			if (respuesta == JFileChooser.APPROVE_OPTION) {
				mostrarArchivos(fc.getSelectedFiles());
				//setScreen(fc.getSelectedFiles());
			}

		}

	}
	
	private void setScreen(File[] files){
		this.files = new File[files.length];
		this.files = files;
		listModel.removeAllElements();
		
		for (int i = 0; i < files.length; i++) {
			listModel.addElement(files[i].getName());
		}
		
		list.setSelectedIndex(0);
		
		
	}
	
	private void mostrarArchivos(File[] files){
		for (int i = 0; i < files.length; i++) {
			System.out.println(files[i].getAbsolutePath());
		}
		
		
	}
}
